<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\admin\model\product\Category as ProductCategory;
use app\admin\model\env\Category as EnvCategory;
use fast\Tree;

class Category extends Api{
    protected $noNeedRight = '*';
    protected $noNeedLogin = '*';
    public function _initialize()
    {
        parent::_initialize();
    }
//    public function list(){
//        $type = $this->request->get('type');
//        $pid = $this->request->get('pid', 0);
//        $list = [];
//        if ($type == 'product'){
//            $list = ProductCategory::all(["pid"=>$pid]);
//        }else if($type == 'env'){
//            $list = EnvCategory::all(["pid"=>$pid]);
//        }
//        $this->success("", $list);
//    }
    public function tree(){
        $type = $this->request->get('type');
        $list = [];
        $tree = new Tree();
        if ($type == 'product'){
            $list = ProductCategory::all();
        }else if($type == 'env'){
            $list = EnvCategory::all();
        }else{
            $this->success("",[
                "product"=> $tree->init(ProductCategory::all(), 'pid')->getTreeArray(0),
                "env"=> $tree->init(EnvCategory::all(), 'pid')->getTreeArray(0),
                ]);
        }
        $tree->init($list, 'pid');
        $this->success("", $tree->getTreeArray(0));
    }


    /**修改分类接口
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author: zhaoyu
     * @Time: 2024/1/18 16:32
     */
    public function list(){
        $params = $this->request->param();
        $where=[];
        if(isset($params['type']) && $params['type'] =='product'){
            $where['type']  =2;
        }
        if(isset($params['type']) && $params['type'] =='env'){
            $where['type']  =1;
        }
        $list = ProductCategory::where($where)->order(['type'=>'asc','id'=>'asc'])->select();
        $tree = new Tree();
        $tree->init($list, 'pid');
        $arr= $tree->getTreeArray(0);
        $this->success("", $arr);
    }






}
